﻿namespace UFIDA.U9.Cust.SH.SHFastShipSV
{
	using System;
	using System.Collections.Generic;
	using System.Text; 
	using UFSoft.UBF.AopFrame;	
	using UFSoft.UBF.Util.Context;
    using UFIDA.U9.Cust.SH.SHFastShipBE;

	/// <summary>
	/// MDCreateFastShipSV partial 
	/// </summary>	
	public partial class MDCreateFastShipSV 
	{	
		internal BaseStrategy Select()
		{
			return new MDCreateFastShipSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class MDCreateFastShipSVImpementStrategy : BaseStrategy
	{
		public MDCreateFastShipSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			MDCreateFastShipSV bpObj = (MDCreateFastShipSV)obj;
			
			//get business operation context is as follows
			//IContext context = ContextManager.Context	

            if (bpObj.FastShipDTOS == null || bpObj.FastShipDTOS.Count==0) throw new Exception("入口参数为空！");
            foreach (FastShipDTO dto in bpObj.FastShipDTOS)
            {
                if (dto.Lines == null || dto.Lines.Count == 0) 
                    throw new Exception("出货单行不能为空！");
                
                //创建快速出货
                CreateFastShip(dto);
            }

            return null;
		}

        private void CreateFastShip(FastShipDTO dto)
        {
            int lineno = 10;

            SHFastShip ship = SHFastShip.Create();
            ship.DocTypeKey = dto.DocType.Key;
            ship.DocNo = dto.DocNo;
            ship.CustomerKey = dto.Customer.Key;
            ship.DocDate = dto.DocDate;
            ship.SaleWHKey = dto.SaleWH.Key;
            ship.IsZY = dto.IsZY;
            ship.ZYCustomerKey = dto.ZYCustomer.Key;
            ship.SaleTypeKey = dto.SaleType.Key;
            ship.DocState = dto.DocState;
            ship.OrgKey = dto.Org.Key;
            ship.WHKey = dto.WH.Key;
            ship.Memo = dto.Memo;
            ship.DeptKey = dto.Dept.Key;
            ship.CreditNoCheck = dto.CreditNoCheck;
            ship.MDDocNo = dto.MDDocNo;
            ship.GSCheck = dto.GSCheck;
            ship.KHCheck = dto.KHCheck;
            ship.MDMemo = dto.MDMemo;
            foreach (FastShipLineDTO line in dto.Lines)
            {
                CreateFastShipLine(ship,line,lineno);
                lineno += 10;
            }

        }

        private void CreateFastShipLine(SHFastShip ship,FastShipLineDTO dto,int lineno)
        {
            SHFastShipLine line = SHFastShipLine.Create(ship);
            
            if (dto.DocLineNo > 0)
                line.DocLineNo = dto.DocLineNo;
            else
                line.DocLineNo = lineno;
            line.ItemKey = dto.Item.Key;
            line.Qty = dto.Qty;
            line.UOMKey = dto.UOM.Key;
            line.WHKey = dto.WH.Key;
            line.Memo = dto.Memo;
            line.OrderQty = dto.OrderQty;
            line.SalePrice = dto.SalePrice;
            line.MDItemCode = dto.MDItemCode;
            line.MDLineID = dto.MDLineID;
        }		
	}

	#endregion
	
	
}